<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../common.inc" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="./css/style1.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="./js/calendar.js"></script>
<script type="text/javascript" src="./js/form.js"></script>
<script type="text/javascript" language="javascript">
<!--
	function checkForm(){
		var invoiceCosts = document.getElementsByName("invoiceCost");
		var invoiceDates = document.getElementsByName("invoiceDate");
		var invoicePersons = document.getElementsByName("invoicePerson");
		var invoiceDescs = document.getElementsByName("invoiceDesc");
		
		var sign = 0;

		
		return true;
	}

	function back(){
		window.location.href = "./contract.do";
		window.location.target = "main";
	}
	
	function personDialog(rowNum){
		var url = "./contract.do?action=selectSalePerson";
		var dlgParam = "dialogHeight:350px;dialogWidth:400px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		var rt = window.showModalDialog(url,"",dlgParam);
		if(rt != undefined){
			//根据从子窗口得到的值，从中提取销售代表id和销售代表名称
			var invoicePersonId = "";
			var invoicePerson = "";
			var tStr = rt.split(",");
			if(tStr[0] != 0 && tStr[1] != 0){
				invoicePersonId = tStr[0];
				invoicePerson = tStr[1];
			}
			document.getElementById("invoicePersonId").value = invoicePersonId;
			document.getElementById("invoicePerson").value = invoicePerson;

		}
	}
	
	function getTotalInvoiceCost(obj){
		onlyNumFloat(obj);
		var totalInvoice = 0;
		var invoiceCosts = document.getElementsByName("invoiceCost");
		for(var i = 0; i < invoiceCosts.length; i++){
			totalInvoice += Number(invoiceCosts[i].value);
		}
		document.getElementById("totalInvoiceCost").innerText = totalInvoice;
		var subInvoiceCost = "<c:out value='${contract.contractCost - totalInvoiceCost}'/>";
		var curSubInvoiceCost = Number(subInvoiceCost) - Number(totalInvoice);
		document.getElementById("subCost").innerText = curSubInvoiceCost;
		if(curSubInvoiceCost < 0){
			//alert("开票金额已超出合同金额！");
			document.getElementById("subInfo").innerText = "开票金额已超出合同金额！";
		}else{
			document.getElementById("subInfo").innerText = "";
		}
	}
	
	function save(){
		if(checkForm()){
			document.addInvoiceForm.action = "./contract.do?action=addInvoiceNew";
			document.addInvoiceForm.target = "main";
			document.addInvoiceForm.submit();
		}
	}
	
	//追加行start
	function addContract(tableId, contractId){//增加tab的一行	
			var url = "./contract.do?action=selectContract&contractId="+contractId;
			var dlgParam = "dialogHeight:350px;dialogWidth:400px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
			var vcontractCode = window.showModalDialog(url,"",dlgParam);
			var vcaseCode;
			var vDescription;
			var vOfficialCost;
			var vOperationCost;c
			var vProxyCost;
			if(vcaseCode != undefined)
			{
				$.getJSON("./contract.do?action=selectContractCaseList",
						  { 
								contractCode:vcontractCode //发送给数据库的数据
						  },
						 function(data) {
						  $.each(data, function(key, val) {
						        if(key=='caseCode'){
						        	vcaseCode = val;
						        }	
						        if(key=='description'){
						        	vDescription =val;
						        }
						        if(key=='officialCost'){
						        	vOfficialCost = val;
						        }
		                        if(key=='operationCost'){
		                        	vOperationCost = val;
						        }
		                        if(key=='proxyCost'){
		                        	vProxyCost = val;
						        }		                        
						        if(vcaseCode!=null && vDescription!=null && vOfficialCost!=null && vOperationCost!=null && vProxyCost!=null){
						        	var insertTdHtml = prepareInsertHtml(tableId,newTrNum,vcaseCode,vDescription,vOfficialCost,vOperationCost,vProxyCost);//获得新增行各个TD的HTML
									var mark = vcaseCode+'_'+newTrNum;
						        	addRow(tableId,insertHtml,mark);
						        }
						    	
						  });
						});
			}
	}
			
	function addRow(tableId,insertTdHtml,mark){//增加tab的一行	
		try{			
			var objT =  document.getElementById(tableId);//通过ID 得到对象
			if(objT==null){	alert("没有可操作的表！");return;	}
			if(objT.rows.length<1){ alert("没有表格标题行"); return;}
			
			var newTr = objT.insertRow(-1);//插入一行
			newTr.className = "table_style";	//为新行赋样式
			var colsLength = objT.rows(0).cells.length;  //表中列总数 
			var newTd ;
			var hiddenObj;		
			var insertTdHtml = prepareInsertHtml(newTrNum);//获得新增行各个TD的HTML代码
			var i=0;
			for(i=0;i<colsLength;i++){
				newTd = newTr.insertCell();
				newTd.innerHTML = insertTdHtml[i];
				newTd.align="left";				
			}
			newTrNum++;//表格总行数加1
		}catch(Exception){}
	}
	
	//增加行所需代码
	var newTrNum = 0;//表格最大行号，只增不减
	var insertHtml = new Array();//用于插入新记录的HTML
	function prepareInsertHtml(num){
		insertHtml[0] = "<input id='invoiceCost' name='invoiceCost' class='input_percent' type='text' onkeyup='getTotalInvoiceCost(this)' onblur='getTotalInvoiceCost(this)' />";
		
		var invoiceDate = "<fmt:formatDate value='<%=new java.util.Date()%>' pattern='yyyy-MM-dd'/>";
		insertHtml[1] = "<input id='invoiceDate' name='invoiceDate' class='input_percent' type='text' value='"+invoiceDate+"' onclick=calendar(this,'yyyy-MM-dd'); readonly='readonly' />";
		
		insertHtml[2] = "&nbsp;<input id='invoicePerson' name='invoicePerson' class='input_percent' type='text' style='width: 75%;' readonly='readonly' />";
		insertHtml[2] += "<input id='invoicePersonId' name='invoicePersonId' type='hidden' />";
		insertHtml[2] += "&nbsp;&nbsp;<input class='btn_select' type='button' value='' onclick='personDialog("+num+")' />";
		
		insertHtml[3] = "<input id='invoiceDesc' name='invoiceDesc' class='input_percent' type='text' />";
		
		return insertHtml;
	}
	//追加行end
	
	function initPage(){
		newTrNum = table1.rows.length;//表格最大行号，只增不减
		
		if("<c:out value='${result}'/>" == 1){
			alert("<fmt:message key='common.message.addSuccess'/>");
		}
	}
	
	function calculateCost(caseId)
	{
		var officialCostAry = document.getElementsByName("officialCostCheck");
		var operationCostAry = document.getElementsByName("operationCostCheck");
		var proxyCostAry = document.getElementsByName("proxyCostCheck");
		
		var officialCostIds = "";
		var operationCostIds = "";
		var proxyCostIds = "";
		
		var costTotal = 0.0;
		for(var i=0; i<officialCostAry.length; i++)
		{
			if(officialCostAry[i].checked)
			{
				costTotal += parseFloat(officialCostAry[i].value);				
				officialCostIds += caseId+"/";	
			}
		}
		if(officialCostIds.length > 0)
			officialCostIds = officialCostIds.substring(0, officialCostIds.length-1);
		
		for(var i=0; i<operationCostAry.length; i++)
		{
			if(operationCostAry[i].checked)
			{
				costTotal += parseFloat(operationCostAry[i].value);				
				operationCostIds += caseId+"/";	
			}
		}
		if(operationCostIds.length > 0)
			operationCostIds = operationCostIds.substring(0, operationCostIds.length-1);
		
		for(var i=0; i<proxyCostAry.length; i++)
		{
			if(proxyCostAry[i].checked)
			{
				costTotal += parseFloat(proxyCostAry[i].value);				
				proxyCostIds += caseId+"/";				
			}

		}
		if(proxyCostIds.length > 0)
			proxyCostIds = proxyCostIds.substring(0, proxyCostIds.length-1);
		
		document.getElementById("officialCostIds").value = officialCostIds;
		document.getElementById("operationCostIds").value = operationCostIds;
		document.getElementById("proxyCostIds").value = proxyCostIds;
				
		document.getElementById("costTotal").value = costTotal;
	}
-->
</script>
</head>
<body onload="initPage()">
<form name="addInvoiceForm" action="" method="post">
<input id="contractId" name="contractId" type="hidden" value="${contractId}"/>
	<table class="table_frame">
		<tr>
			<td>
				<table class="table_title1">
					<tr>
						<td class="title_lab" colspan="2">
							开发票
						</td>
					</tr>
					<tr>
						<td align="center" valign="bottom">
							<input class="btn_submit" type="button" value="提交" onclick="save()" hidefocus="true"/>
							&nbsp;&nbsp;
							<input class="btn_back" type="button" value="取消并返回" onclick="back()" hidefocus="true"/>
						</td>
					</tr>
					<tr><td height="2"></td></tr>
				</table>
			</td>
		</tr>
		<tr>
			<td valign="top">
				<table class="table_title2">
					<tr>
						<td>
							<table class="table_add">
								<tr>
									<td class="title_top">开票日期</td>
									<td class="title_top">领票人</td>
									<td class="title_top">发票号码</td>
									<td class="title_top">总金额</td>
									<td class="title_top">开票金额</td>
									<td class="title_top">备注信息</td>
								</tr>								
								<tr>
									<td>
										<input id="invoiceDate" name="invoiceDate" type="text" style="width:100px"
											onclick="calendar(this,'yyyy-MM-dd');" readonly="readonly" value="${invoice.invoiceDate}"/>
									</td>
									<td>
										<c:choose>
											<c:when test="${isViewAllContract == 1}">
											<input id="invoicePerson" name="invoicePerson" type="text" value="${invoice.invoicePerson}" readonly="readonly"/>
											</c:when>
											<c:otherwise>${userName}</c:otherwise>
										</c:choose>
										<input id="invoicePersonId" name="invoicePersonId" type="hidden" value="${invoice.invoicePersonId}"/>
										<c:if test="${isViewAllContract == 1}">
										<input class="btn_select" type="button" value="" onclick="personDialog()" hidefocus="true"/>
										</c:if>									
									</td>
									<td>
										<input id="invoiceCode" name="invoiceCode" type="text" style="width:100px" value="${invoice.invoiceCode}"/>
									</td>
									<td>
										<input id="costTotal" name="costTotal" type="text" style="width:100px" value="" readonly/>
									</td>
									<td>
										<input id="invoiceTotal" name="invoiceTotal" type="text" style="width:100px" value="${invoice.invoiceTotal}"/>
									</td>			
									<td>
										<input id="invoiceDesc" name="invoiceDesc" type="text" style="width:100px" value="${invoice.invoiceDesc}"/>
									</td>																	
								</tr>								
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table class="table_title2">
					<tr>
						<td>
							<table class="table_add">
								<tr>
									<td class="title_top" width="60">合同明细</td>
									<td class="title_top" width="200">
										<input class="btn_append" type="button" value="添加合同" onclick="addContract('table1', ${contractId})" hidefocus="true"/>
									</td>
									<td class="title_top" colspan="2"><span id="subInfo" style="color: red;"></span></td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td valign="top">
							<table id="table1" class="table_style">
								<tr>
									<td class="title" width="15%">合同号</td>
									<td class="title" width="15%">商品名称</td>
									<td class="title" width="23%">官费</td>
									<td class="title" width="23%">业务费用</td>
									<td class="title" width="23%">代理费用</td>
								</tr>
								<c:forEach var="contractCase" items="${contractCases}">
									<tr>
										<td>
											<input id="conId" name="conId" class="input_percent" type="text" value="${contractCase.contract.contractCode}" readonly/>
										</td>
										<td>
											<input id="contractCaseFullName" name="contractCaseFullName" type="text" value="${contractCase.contractCaseFullName}" readonly/>
										</td>		
										<td>
											<input id="officialCostCheck" name="officialCostCheck" type="checkbox" onClick="javascript:calculateCost(${contractCase.id});" value="${contractCase.officialCost}"/>
											&nbsp;&nbsp;
											<input id="officialCost" name="officialCost" type="text" value="${contractCase.officialCost}" readonly/>
										</td>	
										<td>											
											<input id="operationCostCheck" name="operationCostCheck" type="checkbox" onClick="javascript:calculateCost(${contractCase.id});" value="${contractCase.operationCost}"/>
											&nbsp;&nbsp;
											<input id="operationCost" name="operationCost" type="text" value="${contractCase.operationCost}" readonly/>
										</td>		
										<td>
											<input id="proxyCostCheck" name="proxyCostCheck" type="checkbox" onClick="javascript:calculateCost(${contractCase.id});" value="${contractCase.proxyCost}"/>
											&nbsp;&nbsp;
											<input id="proxyCost" name="proxyCost" type="text" value="${contractCase.proxyCost}" readonly/>
										</td>																																				
									</tr>
								</c:forEach>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
	<input id="officialCostIds" name="officialCostIds" type="hidden" value=""/>
	<input id="operationCostIds" name="operationCostIds" type="hidden" value=""/>
	<input id="proxyCostIds" name="proxyCostIds" type="hidden" value=""/>
</form>
</body>
</html>